﻿@using lsc.Model
@{
    ViewData["Title"] = "添加用户";
    Layout = "~/Pages/_Layout.cshtml";
    List<UserRole> rolelist = ViewBag.userRolelist;
    UserInfo userInfo = ViewBag.userInfo;
}

<blockquote class="layui-elem-quote">
    添加用户信息
</blockquote>
<form class="layui-form" method="post" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">用户名：</label>
        <div class="layui-input-inline">
            <input type="text" name="UserName" value="@(userInfo!=null ? userInfo.UserName:"")" required lay-verify="required|username" placeholder="请输入用户名"  autocomplete="off"  class="layui-input layui-form-text"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-inline">
            <input type="password" name="Password" lay-verify="required|pass" placeholder="请输入密码" autocomplete="off" class="layui-input" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">姓名：</label>
        <div class="layui-input-inline">
            <input type="text" name="Name" value="@(userInfo!=null ? userInfo.Name:"")" required lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input" />
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-inline">
            <input type="text" name="TelPhone" value="@(userInfo!=null ?userInfo.TelPhone:"")" lay-verify="phone" placeholder="请输入手机号" autocomplete="off" class="layui-input" />
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">角色</label>
        <div class="layui-input-inline">
            <select name="RoleID">
                @if (rolelist!=null)
                {
                    foreach (var role in rolelist)
                    {
                        <option value="@(role.ID)" @(userInfo!=null && userInfo.RoleID==role.ID ? "selected":"")>@role.RoleName</option>
                     }
                }
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <input type="hidden" name="ID" value="@(userInfo!=null ? userInfo.ID.ToString():"")" />
            <button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
@section Scripts{
    <script type="text/javascript">
        layui.use(['form','layer'], function () {
            var form = layui.form;
            var layer = layui.layer
            form.verify({
                username: function (value, item) { //value：表单的值、item：表单的DOM对象
                    if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                        return '用户名不能有特殊字符';
                    }
                    if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                        return '用户名首尾不能出现下划线\'_\'';
                    }
                    if (/^\d+\d+\d$/.test(value)) {
                        return '用户名不能全为数字';
                    }
                }

                //我们既支持上述函数式的方式，也支持下述数组的形式
                //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
                , pass: [
                    /^[\S]{6,12}$/
                    , '密码必须6到12位，且不能出现空格'
                ]
            });
            form.on('submit(*)', function (data) {
                console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
                console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
                console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
                $.post('/Users/SaveUser', data.field, function (res) {
                    if (res.code == 1) {
                        layer.msg('保存成功', { icon: 6 });
                        window.location ='/Users/UserList'
                    } else {
                        layer.msg('保存失败', { icon: 5 });
                    }
                })
                return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
            });
        });
    </script>
}
